package nc.bs.generate.impl;

import nc.bs.dao.BaseDAO;
import nc.bs.generate.ift.IFIPGenerateService;
import nc.vo.pub.BusinessException;

import java.time.LocalDate;
import java.util.Map;
/**
 * @author wangyong
 * @date 2025-08-21
 */
public class FIPGenerateServiceImpl implements IFIPGenerateService {
    private BaseDAO baseDAO;
    @Override
    public  Boolean voucherWriteBackBill(Map valMap) throws BusinessException{
        int i = this.getBaseDAO().executeUpdate(this.buildBillWriteBackSQL(valMap));
        return i > 0 ? true : false ;
    }

    private String buildBillWriteBackSQL(Map valMap) throws BusinessException{
        StringBuilder sql = new StringBuilder();
        sql.append(" merge into fip_relation fr \n" );
        sql.append("using gl_voucher glv \n" );
        sql.append("on (  glv.dr = 0  and fr.dr = 0 and fr.des_billtype = 'C0' \n" );
        if (null != valMap.get("pk_accountingbook") ){
            sql.append(" and glv.pk_accountingbook = '"+valMap.get("pk_accountingbook")+"' ");
        }
        if (null != valMap.get("pk_org") ){
            sql.append(" and glv.pk_org = '"+valMap.get("pk_org")+"' ");
        }
        int year = LocalDate.now().getYear();
        sql.append(" and glv.YEAR = "+year+" ");
        sql.append(" and glv.pk_voucher = fr.des_relationid  )  ");
        sql.append(" when matched then \n" );
        sql.append("  update set fr.BUSIMESSAGE10 = glv.free2 , fr.busimessage9 = glv.NUM ");
        return sql.toString();
    }

    public BaseDAO getBaseDAO() {
        return this.baseDAO == null ? new BaseDAO() : this.baseDAO;
    }
}
